/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Collir.java
 *
 * Created on 15/03/2011, 12:06:31
 */
package vistas;

import Renderers.LabelRenderer;
import comu.Configuracio;
import comu.Utilitats;
import controlador.FitosanitariControlador;
import controlador.ModeAplicacioControlador;
import controlador.ProduccioControlador;
import controlador.TractamentControler;
import controlador.UnitatsMesuraControlador;
import controlador.autogenerats.exceptions.IllegalOrphanException;
import controlador.autogenerats.exceptions.NonexistentEntityException;
import entity.Abonat;
import entity.Fitosanitari;
import entity.ModeAplicacio;
import entity.Produccio;
import entity.Tractament;
import java.io.IOException;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import vistas.dialogs.DialogSituarParcela;
import vistas.textImg.Textos;

/**
 *
 * @author David
 */
public final class Tractar extends javax.swing.JFrame implements Observer {

    JFrame menu;
    Tractament tractament;
    Produccio produccio;
    FitosanitariControlador fitosanitaris;
    TractamentControler tractaments;
    ProduccioControlador produccions;
    ModeAplicacioControlador modesAplicacio;
    UnitatsMesuraControlador unitatsMesura;
    DialogSituarParcela situarParcela;

    /** Creates new form Collir */
    Tractar(JFrame menu, Tractament tractament, Produccio produccio) {
        this.menu = menu;
        this.tractament = tractament;
        this.produccio = produccio;

        unitatsMesura = new UnitatsMesuraControlador();
        modesAplicacio = new ModeAplicacioControlador();
        produccions = new ProduccioControlador();
        fitosanitaris = new FitosanitariControlador();
        tractaments = new TractamentControler();

        initComponents();
        llistaFitos.setCellRenderer(new LabelRenderer());
        Utilitats.centrarVentana(this);
        try {
            if (this.tractament != null) {
                update(null, this.tractament);
            } else if (this.produccio != null) {
                update(null, this.produccio);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        llistaFitos = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        btAvui = new javax.swing.JButton();
        lblTotal = new javax.swing.JLabel();
        btCancelar = new javax.swing.JButton();
        lblDosis = new javax.swing.JLabel();
        ftfTotal = new javax.swing.JFormattedTextField();
        ftfDosis = new javax.swing.JFormattedTextField();
        lblUnitatDisolcio = new javax.swing.JLabel();
        ftfQuantitatDisolucio = new javax.swing.JFormattedTextField();
        btAceptar = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        btMenu = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        lblMapaCamp = new javax.swing.JLabel();
        dcDataTractament = new com.toedter.calendar.JDateChooser();
        jLabel10 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        lblMateriaActiva = new javax.swing.JLabel();
        lblProdAutor = new javax.swing.JLabel();
        lblNomFitos = new javax.swing.JLabel();
        cbModeAplicacio = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        taObservacions = new javax.swing.JTextArea();
        lblComposicioFito = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        lblPlazSeguretat = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        lblNumReg = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        lblDataFi = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Tractament");

        llistaFitos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        llistaFitos.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                llistaFitosValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(llistaFitos);

        jLabel3.setText(Textos.TRACTAR_LABEL_DATA_TRACTAMENT);

        btAvui.setText(Textos.TRACTAR_BOTO_AVUI);
        btAvui.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAvuiActionPerformed(evt);
            }
        });

        lblTotal.setText(Textos.TRACTAR_LABEL_TOTAL);

        btCancelar.setText(Textos.COMU_BOTO_CANCELAR);
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        lblDosis.setText(Textos.TRACTAR_LABEL_DOSIS);

        ftfTotal.setText("0");
        ftfTotal.setFont(Textos.FONT_GRAN);

        ftfDosis.setEditable(false);
        ftfDosis.setText("0");
        ftfDosis.setEnabled(false);
        ftfDosis.setFont(Textos.FONT_GRAN);
        ftfDosis.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                ftfQuantitatDisolucioKeyTyped(evt);
            }
        });

        lblUnitatDisolcio.setText(Textos.TRACTAR_LABEL_DISOL);

        ftfQuantitatDisolucio.setText("0");
        ftfQuantitatDisolucio.setFont(Textos.FONT_GRAN);
        ftfQuantitatDisolucio.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                ftfQuantitatDisolucioKeyTyped(evt);
            }
        });

        btAceptar.setText(Textos.COMU_BOTO_ACCEPTAR);
        btAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAceptarActionPerformed(evt);
            }
        });

        jLabel7.setText(Textos.TRACTAR_LABEL_MODE_APLIC);

        btMenu.setText(Textos.COMU_BOTO_MENU);
        btMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btMenuActionPerformed(evt);
            }
        });

        jLabel8.setText(vistas.textImg.Textos.TRACTAR_LABEL_FITOS);

        lblMapaCamp.setText(" ");
        lblMapaCamp.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblMapaCampMouseClicked(evt);
            }
        });

        dcDataTractament.setDate(GregorianCalendar.getInstance().getTime());

        jLabel10.setText(Textos.TRACTAR_LABEL_NOM);

        jLabel15.setText(Textos.TRACTAR_LABEL_MATERIA_ACTIVA);

        jLabel16.setText(Textos.TRACTAR_LABEL_PROD_AUTO);

        lblMateriaActiva.setText("   ");

        lblProdAutor.setText("   ");

        lblNomFitos.setText("   ");

        cbModeAplicacio.setModel(Utilitats.getComboBoxModel(modesAplicacio.findModeAplicacioEntities()));
        cbModeAplicacio.setAutoscrolls(true);
        cbModeAplicacio.setRenderer(new LabelRenderer());

        jLabel9.setText(Textos.TRACTAR_LABEL_OBSERVACIONS);

        taObservacions.setColumns(20);
        taObservacions.setRows(5);
        jScrollPane2.setViewportView(taObservacions);

        lblComposicioFito.setText("   ");

        jLabel17.setText(Textos.TRACTAR_LABEL_COMPOSICIO);

        lblPlazSeguretat.setText("   ");

        jLabel18.setText(Textos.TRACTAR_LABEL_PLAZ_SEG);

        lblNumReg.setText("   ");

        jLabel19.setText(Textos.TRACTAR_LABEL_NUM_REG);

        lblDataFi.setText("   ");

        jLabel20.setText(Textos.TRACTAR_LABEL_DATA_FI);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 328, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(layout.createSequentialGroup()
                                .add(jLabel10)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(lblNomFitos))
                            .add(layout.createSequentialGroup()
                                .add(jLabel15)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(lblMateriaActiva))
                            .add(layout.createSequentialGroup()
                                .add(jLabel17)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(lblComposicioFito))
                            .add(layout.createSequentialGroup()
                                .add(jLabel18)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(lblPlazSeguretat))
                            .add(layout.createSequentialGroup()
                                .add(jLabel19)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(lblNumReg))
                            .add(jLabel16)
                            .add(layout.createSequentialGroup()
                                .add(jLabel20)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(lblDataFi))
                            .add(layout.createSequentialGroup()
                                .add(24, 24, 24)
                                .add(lblProdAutor)))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                .add(jLabel3)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(dcDataTractament, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 153, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(btAvui, 0, 0, Short.MAX_VALUE)
                                .add(15, 15, 15))
                            .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(layout.createSequentialGroup()
                                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                            .add(lblDosis)
                                            .add(lblTotal)
                                            .add(lblUnitatDisolcio))
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                            .add(ftfQuantitatDisolucio)
                                            .add(ftfTotal)
                                            .add(ftfDosis, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 240, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                                    .add(jLabel9)
                                    .add(layout.createSequentialGroup()
                                        .add(jLabel7)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                        .add(cbModeAplicacio, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 247, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap())
                            .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE)
                                .addContainerGap())
                            .add(layout.createSequentialGroup()
                                .add(lblMapaCamp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 202, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                        .add(org.jdesktop.layout.GroupLayout.TRAILING, btCancelar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 129, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .add(org.jdesktop.layout.GroupLayout.TRAILING, btAceptar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 129, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                    .add(btMenu, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 129, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                .addContainerGap())))
                    .add(layout.createSequentialGroup()
                        .add(jLabel8, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 180, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(520, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(jLabel8)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(btAvui, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                .add(dcDataTractament, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
                                .add(jLabel3)))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(lblUnitatDisolcio)
                            .add(ftfQuantitatDisolucio, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(lblDosis)
                            .add(ftfDosis, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 38, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(ftfTotal, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 39, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(lblTotal))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel7)
                            .add(cbModeAplicacio, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel9)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 133, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 363, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel10)
                            .add(lblNomFitos))))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(lblMapaCamp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 173, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(layout.createSequentialGroup()
                        .add(btAceptar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btCancelar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 32, Short.MAX_VALUE)
                        .add(btMenu, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 39, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel15)
                            .add(lblMateriaActiva))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel17)
                            .add(lblComposicioFito))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel18)
                            .add(lblPlazSeguretat))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel19)
                            .add(lblNumReg))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel20)
                            .add(lblDataFi))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel16)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(lblProdAutor)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        if (produccio != null) {
            produccio = null;
            btMenuActionPerformed(evt);
        } else {
            produccio = null;
            update_Fitos();
        }
    }//GEN-LAST:event_btCancelarActionPerformed

    private void btAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAceptarActionPerformed

        Fitosanitari fitosanitari = (Fitosanitari) llistaFitos.getSelectedValue();
        if (fitosanitari != null) {
            if (produccio != null) {
                Date data = dcDataTractament.getDate();
                float disolucio = Float.valueOf(ftfQuantitatDisolucio.getText());
                if (disolucio != 0) {
                    float dosis = Float.valueOf(ftfDosis.getText());
                    if(dosis != 0){
                        float quantitat = Float.valueOf(ftfTotal.getText());
                        if (quantitat != 0) {
                            ModeAplicacio modeAplicio = (ModeAplicacio) cbModeAplicacio.getSelectedItem();
                            if (modeAplicio != null) {
                                if (tractament == null) {
                                    tractament = new Tractament(null);
                                    tractament.setDataTractament(data);
                                    tractament.setQuantitatDisolucio(disolucio);
                                    tractament.setDosis(dosis);
                                    tractament.setQuantitatTotal(quantitat);
                                    tractament.setModeAplicacioId(modeAplicio);
                                    tractament.setObservacions(taObservacions.getText());
                                    tractament.setFitosanitariId(fitosanitari);
                                    tractament.setProduccioId(this.produccio);
                                    tractaments.create(tractament);
                                    btCancelarActionPerformed(evt);
                                    update(null, null);
                                } else {
                                    tractament.setDataTractament(data);
                                    tractament.setQuantitatDisolucio(disolucio);
                                    tractament.setDosis(dosis);
                                    tractament.setQuantitatTotal(quantitat);
                                    tractament.setModeAplicacioId(modeAplicio);
                                    tractament.setObservacions(taObservacions.getText());
                                    tractament.setFitosanitariId(fitosanitari);
                                    try {
                                        tractaments.edit(tractament);
                                        btCancelarActionPerformed(evt);
                                        Thread.sleep(200);
                                    } catch (NonexistentEntityException ex) {
                                        Logger.getLogger(Tractar.class.getName()).log(Level.SEVERE, null, ex);
                                    } catch (Exception ex) {
                                        Logger.getLogger(Tractar.class.getName()).log(Level.SEVERE, null, ex);
                                    }
                                    btMenuActionPerformed(evt);
                                }
                            } else {
                                JOptionPane.showMessageDialog(this, Textos.TRACTAR_INFO_MODE_APLIC, "", JOptionPane.WARNING_MESSAGE);
                            }
                        } else {
                            JOptionPane.showMessageDialog(this, Textos.TRACTAR_INFO_TOTAL, "", JOptionPane.WARNING_MESSAGE);
                        }
                    }else{
                        JOptionPane.showMessageDialog(this, Textos.TRACTAR_INFO_DOSIS, "", JOptionPane.WARNING_MESSAGE);
                    }
                }else{
                    JOptionPane.showMessageDialog(this, Textos.TRACTAR_INFO_DISOLUCIO, "", JOptionPane.WARNING_MESSAGE);
                }
            } else {
                JOptionPane.showMessageDialog(this, Textos.TRACTAR_INFO_PRODUCCIO, "", JOptionPane.WARNING_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(this, Textos.TRACTAR_INFO_FITO, "", JOptionPane.WARNING_MESSAGE);
        }
}//GEN-LAST:event_btAceptarActionPerformed

    private void btMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btMenuActionPerformed
        if (menu != null) {
            if (General.class.isInstance(menu)) {
                ((General) menu).update(null, null);
            }
            menu.setVisible(true);
            this.setVisible(false);
        }
    }//GEN-LAST:event_btMenuActionPerformed

    private void ftfQuantitatDisolucioKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_ftfQuantitatDisolucioKeyTyped
        if ((!ftfQuantitatDisolucio.getText().equals("0")) && (!ftfDosis.getText().equals("0"))) {
            try {
                ftfTotal.setText(String.valueOf(Float.parseFloat(ftfDosis.getText()) * Integer.parseInt(ftfQuantitatDisolucio.getText())));
            } catch (Exception e) {
                ftfTotal.setText("0");
            }
        }
    }//GEN-LAST:event_ftfQuantitatDisolucioKeyTyped

    private void llistaFitosValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_llistaFitosValueChanged
        Fitosanitari fitosanitari = (Fitosanitari) llistaFitos.getSelectedValue();
        if (fitosanitari != null) {
            lblUnitatDisolcio.setText(fitosanitari.getUnitatsDisolucio().getNom());
            ftfDosis.setText(String.valueOf(fitosanitari.getDosis()));
            String nom = fitosanitari.getNomComercial();
            if (nom != null) {
                lblNomFitos.setText(nom);
            }
            String materiaActiva = fitosanitari.getMateriaActiva();
            if (materiaActiva != null) {
                lblMateriaActiva.setText(materiaActiva);
            }
            String composicio = fitosanitari.getComposicio();
            if (composicio != null) {
                lblComposicioFito.setText(composicio);
            }
            String plazSeg = String.valueOf(fitosanitari.getPlazSegurDies());
            if (plazSeg != null) {
                lblPlazSeguretat.setText(plazSeg);
            }
            String numReg = fitosanitari.getNumRegistre();
            if (numReg != null) {
                lblNumReg.setText(numReg);
            }
            Date dataFi = fitosanitari.getDataFiAutoritzacio();
            if (dataFi != null) {
                lblDataFi.setText(Configuracio.sdf_ddMMyyyy.format(dataFi));
            }
            String prodAuto = fitosanitari.getProducAutortizats();
            if (prodAuto != null) {
                lblProdAutor.setText(prodAuto);
            }
            String unitatDosis = fitosanitari.getUnitatsDosis().getNom();
            String unitatDisol = fitosanitari.getUnitatsDisolucio().getNom();
            if ((unitatDisol != null) && (unitatDosis != null)) {
                lblDosis.setText(Textos.TRACTAR_LABEL_DOSIS + "(" + unitatDosis + "/" + unitatDisol + ")");
                lblTotal.setText(Textos.TRACTAR_LABEL_TOTAL + "(" + unitatDosis + ")");
            }
            ftfDosis.setText(String.valueOf(fitosanitari.getDosis()));
        }
    }//GEN-LAST:event_llistaFitosValueChanged

    private void lblMapaCampMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblMapaCampMouseClicked
        if (situarParcela == null) {
            List<Produccio> veins = produccions.findProduccioEntitiesByCamp(this.produccio.getCampId(), false);
            veins.remove(produccio);
            situarParcela = new DialogSituarParcela(this, true, this.produccio.getCampId(), produccio, veins, false);
        }
        situarParcela.setVisible(true);
    }//GEN-LAST:event_lblMapaCampMouseClicked

    private void btAvuiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAvuiActionPerformed
        dcDataTractament.setDate(GregorianCalendar.getInstance().getTime());
}//GEN-LAST:event_btAvuiActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAceptar;
    private javax.swing.JButton btAvui;
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btMenu;
    private javax.swing.JComboBox cbModeAplicacio;
    private com.toedter.calendar.JDateChooser dcDataTractament;
    private javax.swing.JFormattedTextField ftfDosis;
    private javax.swing.JFormattedTextField ftfQuantitatDisolucio;
    private javax.swing.JFormattedTextField ftfTotal;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblComposicioFito;
    private javax.swing.JLabel lblDataFi;
    private javax.swing.JLabel lblDosis;
    private javax.swing.JLabel lblMapaCamp;
    private javax.swing.JLabel lblMateriaActiva;
    private javax.swing.JLabel lblNomFitos;
    private javax.swing.JLabel lblNumReg;
    private javax.swing.JLabel lblPlazSeguretat;
    private javax.swing.JLabel lblProdAutor;
    private javax.swing.JLabel lblTotal;
    private javax.swing.JLabel lblUnitatDisolcio;
    private javax.swing.JList llistaFitos;
    private javax.swing.JTextArea taObservacions;
    // End of variables declaration//GEN-END:variables

    public void update(Observable o, Object o1) {
        if (Tractament.class.isInstance(o1)) {
            try {
                update_Fitos();
                this.tractament = (Tractament) o1;
                produccio = this.tractament.getProduccioId();
                Utilitats.adabtarImatgeAlLabel(lblMapaCamp, produccio.getCampId().getImg());
                btMenu.setEnabled(false);
                List<Produccio> veins = produccions.findProduccioEntitiesByCamp(produccio.getCampId(), false);
                veins.remove(produccio);
                situarParcela = new DialogSituarParcela(this, true, produccio.getCampId(), produccio, veins, true);
                llistaFitos.setSelectedValue(tractament.getFitosanitariId(), true);
                dcDataTractament.setDate(tractament.getDataTractament());
                ftfQuantitatDisolucio.setText(String.valueOf(tractament.getQuantitatDisolucio()));
                ftfDosis.setText(String.valueOf(tractament.getDosis()));
                ftfTotal.setText(String.valueOf(tractament.getQuantitatDisolucio()));
                cbModeAplicacio.setSelectedItem(tractament.getModeAplicacioId());
                taObservacions.setText(tractament.getObservacions());
            } catch (IOException ex) {
                Logger.getLogger(Tractar.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (Produccio.class.isInstance(o1)) {
            try {
                update_Fitos();
                this.tractament = null;
                this.produccio = (Produccio) o1;
                Utilitats.adabtarImatgeAlLabel(lblMapaCamp, produccio.getCampId().getImg());
                btMenu.setEnabled(false);
            } catch (IOException ex) {
                Logger.getLogger(Tractar.class.getName()).log(Level.SEVERE, null, ex);
            }

        } else {
            update_Fitos();
            this.tractament = null;
            this.produccio = null;
            btMenu.setEnabled(true);

        }
    }

    private void update_Fitos() {
        llistaFitos.setModel(getListModel(fitosanitaris.findFitosanitariEntitiesAZ()));
        dcDataTractament.setDate(GregorianCalendar.getInstance().getTime());
        lblUnitatDisolcio.setText(Textos.TRACTAR_LABEL_DISOL);
        ftfQuantitatDisolucio.setText("0");
        lblDosis.setText(Textos.TRACTAR_LABEL_DOSIS);
        ftfDosis.setText("0");
        lblTotal.setText(Textos.TRACTAR_LABEL_TOTAL);
        ftfTotal.setText("0");
        cbModeAplicacio.setModel(Utilitats.getComboBoxModel(modesAplicacio.findModeAplicacioEntities()));
        taObservacions.setText("");
        lblNomFitos.setText(" ");
        lblMateriaActiva.setText(" ");
        lblComposicioFito.setText(" ");
        lblPlazSeguretat.setText(" ");
        lblNumReg.setText(" ");
        lblDataFi.setText(" ");
        lblProdAutor.setText(" ");
    }

    private DefaultListModel getListModel(List llista) {
        DefaultListModel modelP = new DefaultListModel();
        for (int i = 0; i < llista.size(); i++) {
            modelP.addElement(llista.get(i));
        }
        return modelP;
    }
}
